﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YJKRevitWallModule.WallModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdSetNewThick : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Transaction transaction = new Transaction(cmdData.Application.ActiveUIDocument.Document, this.ToString());
			transaction.Start();
			bool flag = true;
			WallType wallType = this.m_refWall.WallType;
			CompoundStructure compoundStructure = wallType.GetCompoundStructure();
			IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
			foreach (CompoundStructureLayer compoundStructureLayer in layers)
			{
				if ((int)compoundStructureLayer.Function == 3)
				{
					flag = false;
					compoundStructureLayer.Width = this.m_dNewThick / 304.8;
				}
			}
			if (flag && this.m_dNewThick > 0.0)
			{
				layers.Insert(0, new CompoundStructureLayer
				{
                    Function = (MaterialFunctionAssignment)3,
					Width = this.m_dNewThick / 304.8
				});
			}
			compoundStructure.SetLayers(layers);
			wallType.SetCompoundStructure(compoundStructure);
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Wall m_refWall;

		public double m_dNewThick;
	}
}
